<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <script>

    // 1、找出连续重复次数最多的字符
    // `aaaabbbbbcccccccccdddd`
    // js 里面如果越界，返回undefined
    function findout(str) {
      let i = 0, j = 1, len = str.length
      let maxRepeatCnt = 0
      let maxRepeatCh = ''
      while (i < len) {
        if (str[i] != str[j]) {
          maxRepeatCnt = maxRepeatCnt < j - i ? j - i : maxRepeatCnt
          maxRepeatCh = str[i]
          i = j
        }
        j++
      }
      return maxRepeatCh
    }


    // 2、用递归计算斐波那契数列第n项
    // 这里有缓存重复出现的值的思想
    let cache = {}
    function fib(n) {
      if (cache.hasOwnProperty(n)) {
        return cache[n]
      }

      let v = n == 1 || n == 2 ? 1 : fib(n-1) + fib(n-2)
      cache[n] = v
      return v
    }



    // 3、转化
    // [1, 2, [3, [4, 5], 6], 7]转换为:
/*
    {
      children: [
        { value: 1 },
        { value: 2 },
        { 
          children: [
            { value: 3 },
            { 
              children: [
                { value: 4 },
                { value: 5 }
              ] 
            },
            { value: 6 }
          ] 
        },
        { value: 7 }
      ]
    }
*/
    // fn是自己写的，测试通过了；
    // 也就是视频里老师的第二种解法
    function fn(value) {
      if (typeof value == "number") {
        return { value }
      } else {
        let children = []
        for (let v of value) {
          children.push(fn(v))
        }
        return { children }
      }
    }

    let tar = [1, 2, [3, [4, 5], 6], 7]
    let ret = fn(tar)
    console.log(ret)
  </script>
</body>
</html>